/**
 * create by young (young2f@outlook.com) on 2020/9/16
 */


'use strict';
import React from 'react';
import {Button, Form, Input, notification, Table} from 'antd';

import FormBase from '../../../components/Base/FormBase';
import {connect} from "dva";
import moment from 'moment';
import 'moment/locale/zh-cn';
import DescGroup from "../../../components/DescriptionGroup";
import ViewTitle from "../../../components/ViewTitle";
import {FormField, FormLayout, ModalFooter} from "antd-extra";
import notify from "../../../components/NotificationExtra";
import styles from "../../index.less";

moment.locale('zh-cn');
const table = 'Material';
@Form.create()
@connect(({
            user,
            global,
            material,
            store
          }) => ({...user, ...material, ...global, ...store}))
export default class ModalStoreDetail extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table}
    });
    this.onDataLoad().then(res => {
      this.setState(res);
    });

  }

  async onDataLoad() {
    const {values} = this.props;
    const {code} = values;
    const lotRes = await this.props.dispatch({
      type: 'store/findInboundLots',
      payload: {code}
    });
    const outRes = await this.props.dispatch({
      type: 'store/findOutboundLots',
      payload: {code}
    });
    console.log('inlotsss', lotRes);
    console.log('outLots', outRes);
    return {inLots: lotRes.result, outLots: outRes.result};
  }


  cols() {
    return [
      {title: '批次类型', dataIndex: 'lotType', key: 'lotType'},
      {title: '批次数量', dataIndex: 'lotCount', key: 'lotCount'},
      {
        title: '操作时间', dataIndex: 'lotDate', key: 'lotDate', render: (value) => {
          return <div>{moment(value).format('YYYY-MM-DD')}</div>;
        }
      }
    ];
  }

  render() {
    const {style, values, cols = 3} = this.props;
    const {outLots, inLots} = this.state;
    const model = this.props[`${table}Model`];
    console.log('model', model);
    if (!model) return;

    const {labels = []} = model;
    const _labels = [];
    const _values = _.cloneDeep(values);
    _.map(model, (item, key) => {
      const {enableArea} = item;
      if (key !== 'labels' && _.includes(enableArea, 'detail')) {
        item.key = key;
        _labels.push(item);
      }
    });

    _.map(labels, (item) => {
      const {key, inputType} = item;
      _labels.push(item);

      if (inputType.toLowerCase() === 'switch') {
        const value = _values[key];
        const _v = (value === undefined || value === null || value === "") && value.toLowerCase() !== 'true' ? "否" : '是';
        _values[key] = _v;
      }
    });
    const lots = [];
    _.map(inLots, (item) => {
      item.lotCount = item.inboundCount;
      item.lotDate = item.inboundDate;
      // item.lotType = '入库';
      lots.push(item);
    });
    _.map(outLots, (item) => {
      item.lotCount = item.outboundCount;
      item.lotDate = item.outboundDate;
      lots.push(item);
    });
    const _lots=_.orderBy(lots,['createdTs'], ['desc']);
    console.log('llllll', lots);
    return (
      <Form style={{...style}}>
        <ViewTitle
          title={'物料库存信息'}
          level={1}
        ></ViewTitle>
        <DescGroup
          {...{
            labels: _labels,
            values: _values,
            size: 'small',
            column: cols
          }}
        ></DescGroup>
        {<ViewTitle
          title={'库存批次记录'}
          level={1}
        ></ViewTitle>}
        {<Table
          className={styles.antTable}
          bordered={true}
          columns={this.cols()}
          dataSource={_lots}
          scroll={{x: true}}
          pagination={{pageSize: 100}}
          //scroll={{ x: 1300 }}
        ></Table>}
        {/*{inInbound && <FormLayout form={form} cols={cols}>*/}
        {/*  <FormField*/}
        {/*    label={'入库数量'}*/}
        {/*    name={'inboundCount'}*/}
        {/*    required*/}
        {/*  >*/}
        {/*    <Input style={{width: '100%'}}*/}
        {/*    ></Input>*/}
        {/*  </FormField>*/}
        {/*</FormLayout>}*/}
        {/*{inInbound && this.renderFoot()}*/}
      </Form>

    );
  }
}

